package com.msg.chestionar.entities;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import com.msg.chestionar.to.ChestionarTO;
import com.msg.chestionar.to.IntrebareTO;

@NamedQueries({
	@NamedQuery(name="ChestionarE.findById", query="SELECT u FROM ChestionarE u WHERE u.id = :id"),
	@NamedQuery(name="ChestionarE.findAll", query="SELECT u FROM ChestionarE u"),
	@NamedQuery(name="ChestionarE.findAllActiv", query="SELECT u FROM ChestionarE u WHERE u.activ= '1' and u.id not in (SELECT r.chestionar_id FROM RaspunsuriE r where r.user_id= :loggedInUser )"),
	@NamedQuery(name="ChestionarE.getChestionarByName", query="SELECT c FROM ChestionarE c WHERE c.nume = :nume")
})

@Entity
@Table(name="T_CHESTIONAR")
@SequenceGenerator(name = "MySEQ_CHESTIONAR",sequenceName = "SEQ_CHESTIONAR", allocationSize = 1) 
public class ChestionarE {
	
	@Id
	@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="MySEQ_CHESTIONAR")
	private Long id;
	
	@Column
	private String nume;
	
	@Column
	private String creat_de;
	
	@Column
	private int activ;
	
	@OneToMany (cascade = CascadeType.ALL, fetch=FetchType.EAGER, mappedBy="chestionarE")
	@OrderBy("id")
	private List<IntrebareE> intrebari;
	
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getNume() {
		return nume;
	}
	public void setNume(String nume) {
		this.nume = nume;
	}
	public String getCreat_de() {
		return creat_de;
	}
	public void setCreat_de(String creatDe) 
	{
		creat_de = creatDe;
	}
	public int getActiv() 
	{
		return activ;
	}
	public void setActiv(int activ) {
		this.activ = activ;
	}
	
	
//	public ChestionarTO getChestionarTO()
//	{
//		ChestionarTO chestionar = new ChestionarTO();
//		
//		chestionar.setId(this.id);
//		chestionar.setNume(this.nume);
//		chestionar.setCreat_de(this.creat_de);
//		chestionar.setActiv(this.activ ==1);
//		List<IntrebareTO> intrebariTO=new ArrayList<IntrebareTO>();
//		
//		int i = 0;
//		for(IntrebareE intrE: getIntrebari()){
//			IntrebareTO intr = intrE.getIntrebareTO();
//			intr.setOrderNo(i++);
//			intrebariTO.add(intr);
//		}
//		chestionar.setIntrebari(intrebariTO);
//		
//		return chestionar;
//	}
	
	public ChestionarTO getChestionarTO()
	{
		ChestionarTO chestionar = new ChestionarTO();
		
		chestionar.setId(this.id);
		chestionar.setNume(this.nume);
		chestionar.setCreat_de(this.creat_de);
		chestionar.setActiv(this.activ ==1);
		List<IntrebareTO> intrebariTO=new ArrayList<IntrebareTO>();
		
		int i = 0;
		for(IntrebareE intrE: getIntrebari()){
			IntrebareTO intr = intrE.getIntrebareTO();
			intr.setOrderNo(i++);
			intrebariTO.add(intr);
		}
//		chestionar.setIntrebari(ordonareIntrebari(intrebariTO));
		chestionar.setIntrebari(intrebariTO);
		return chestionar;
	}
	
//	public List<IntrebareTO> ordonareIntrebari(List<IntrebareTO> intreb){
//		int nrIntrebari=intreb.size();
//		int i=1;
//		List<IntrebareTO> intrebariTO=new ArrayList<IntrebareTO>();
//		while (i<=nrIntrebari){
//			for (IntrebareTO intr: intreb){
//				if (intr.getOrdine()==i)
//					intrebariTO.add(intr);	
//			}
//			i++;
//		}
//		return intrebariTO;
//	}
	
	public void setIntrebari(List<IntrebareE> intrebari) {
		this.intrebari = intrebari;
	}
	
	public List<IntrebareE> getIntrebari() {
		return intrebari;
	}

	
	public void removeIntrebare(IntrebareE intrebare) {
		if (this.intrebari!=null){
			return;
		}
		this.intrebari.remove(intrebare);
		
	}
	
	public void addIntrebare(IntrebareE intrebare) {
		if (this.intrebari== null){
			this.intrebari = new ArrayList<IntrebareE>();
		}
		this.intrebari.add(intrebare);
	}
}
